import Vue from 'vue'
import VueRouter from 'vue-router'
// export const EventBus = new Vue();
Vue.use(VueRouter)

//解决vue-router重复点击报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/Login.vue"),

    meta: {
      title: "登录",
    },
  },
  {
    path: "/analysis",
    name: "analysis",
    component: () => import("../components/navbar.vue"),
    meta: {
      title: "首页",
    },
    children: [
      {
        path: "/",
        name: "home",
        component: () => import("../views/home/home.vue"),
        meta: {
          title: "入库管理",
        },
      },
      {
        path: "/cgrk",
        name: "cgrk",
        component: () => import("../views/rkgl/cgrk.vue"),
        meta: {
          title: "采购入库",
        },
      },
      {
        path: "/cgthrk",
        name: "cgthrk",
        component: () => import("../views/rkgl/cgthrk.vue"),
        meta: {
          title: "采购退货入库（红入）",
        },
      },
      {
        path: "/pyrk",
        name: "pyrk",
        component: () => import("../views/rkgl/pyrk.vue"),
        meta: {
          title: "盘营入库",
        },
      },
      {
        path: "/zdrk",
        name: "zdrk",
        component: () => import("../views/rkgl/zdrk.vue"),
        meta: {
          title: "涨吨入库",
        },
      },
      {
        path: "/qtrk",
        name: "qtrk",
        component: () => import("../views/rkgl/qtrk.vue"),
        meta: {
          title: "其他入库",
        },
      },
      {
        path: "/xsck",
        name: "xsck",
        component: () => import("../views/ckgl/xsck.vue"),
        meta: {
          title: "销售出库",
        },
      },
      {
        path: "/pkck",
        name: "pkck",
        component: () => import("../views/ckgl/pkck.vue"),
        meta: {
          title: "盘亏出库",
        },
      },
      {
        path: "/qtck",
        name: "qtck",
        component: () => import("../views/ckgl/qtck.vue"),
        meta: {
          title: "其他出库",
        },
      },
      //其他管理
      {
        path: "/kctb",
        name: "kctb",
        component: () => import("../views/qtgl/kctb.vue"),
        meta: {
          title: "库存调拨",
        },
      },
      {
        path: "/cbtz",
        name: "cbtz",
        component: () => import("../views/qtgl/cbtz.vue"),
        meta: {
          title: "成本调整",
        },
      },
      {
        path: "/kcpd",
        name: "kcpd",
        component: () => import("../views/qtgl/kcpd.vue"),
        meta: {
          title: "库存盘点",
        },
      },
      {
        path: "/wlkc",
        name: "wlkc",
        component: () => import("../views/jskc/wlkc.vue"),
        meta: {
          title: "物料库存",
        },
      },
      {
        path: "/wlpckc",
        name: "wlpckc",
        component: () => import("../views/jskc/wlpckc.vue"),
        meta: {
          title: "物料批次库存",
        },
      },
      {
        path: "/ckwlkc",
        name: "ckwlkc",
        component: () => import("../views/jskc/ckwlkc.vue"),
        meta: {
          title: "仓库物料库存",
        },
      },
      {
        path: "/xxkc",
        name: "xxkc",
        component: () => import("../views/jskc/xxkc.vue"),
        meta: {
          title: "详细库存",
        },
      },
      {
        path: "/crkyhz",
        name: "crkyhz",
        component: () => import("../views/crkgl/crkyhz.vue"),
        meta: {
          title: "出入库月汇总",
        },
      },
      {
        path: "/crkrdz",
        name: "crkrdz",
        component: () => import("../views/crkgl/crkrdz.vue"),
        meta: {
          title: "出入库日汇总",
        },
      },
      {
        path: "/crkmx",
        name: "crkmx",
        component: () => import("../views/crkgl/crkmx.vue"),
        meta: {
          title: "出入库明细",
        },
      },
      {
        path: "/customer",
        name: "customer",
        component: () => import("../views/user.vue/customer.vue"),
        meta: {
          title: "客户",
        },
      },
      {
        path: "/supplier",
        name: "supplier",
        component: () => import("../views/user.vue/supplier.vue"),
        meta: {
          title: "供应商",
        },
      },
      {
        path: "/materialCategory",
        name: "materialCategory",
        component: () => import("../views/user.vue/materialCategory.vue"),
        meta: {
          title: "物料分类",
        },
      },

      {
        path: "/unit",
        name: "unit",
        component: () => import("../views/user.vue/unit.vue"),
        meta: {
          title: "计量单位",
        },
      },
      {
        path: "/currency",
        name: "currency",
        component: () => import("../views/user.vue/currency.vue"),
        meta: {
          title: "币种",
        },
      },
      {
        path: "/bankAccount",
        name: "bankAccount",
        component: () => import("../views/user.vue/bankAccount.vue"),
        meta: {
          title: "银行账户",
        },
      },
      {
        path: "/material",
        name: "material",
        component: () => import("../views/user.vue/material.vue"),
        meta: {
          title: "物料",
        },
      },
      {
        path: "/warehouse",
        name: "warehouse",
        component: () => import("../views/user.vue/warehouse.vue"),
        meta: {
          title: "仓库",
        },
      },
      // 发票登记
      {
        path: "/cgfp",
        name: "cgfp",
        component: () => import("../views/fpdj/cgfp.vue"),
        meta: {
          title: "销售发票",
        },
      },
      {
        path: "/cgfphc",
        name: "cgfphc",
        component: () => import("../views/fpdj/cgfphc.vue"),
        meta: {
          title: "销售发票(红冲)",
        },
      },
      {
        path: "/cgfpth",
        name: "cgfpth",
        component: () => import("../views/fpdj/cgfpth.vue"),
        meta: {
          title: "销售发票(退货)",
        },
      },
      {
        path: "/xsfp",
        name: "xsfp",
        component: () => import("../views/fpdj/xsfp.vue"),
        meta: {
          title: "采购发票",
        },
      },
      {
        path: "/xsfphc",
        name: "xsfphc",
        component: () => import("../views/fpdj/xsfphc.vue"),
        meta: {
          title: "采购发票(红冲)",
        },
      },
      {
        path: "/xsfpth",
        name: "xsfpth",
        component: () => import("../views/fpdj/xsfpth.vue"),
        meta: {
          title: "采购发票(退货)",
        },
      },
      // UserManagement
      {
        path: "/UserManagement",
        name: "UserManagement",
        component: () => import("../views/fpdj/xsfp.vue"),
        meta: {
          title: "销售发票",
        },
      },
      {
        path: "/RoleManagement",
        name: "RoleManagement",
        component: () => import("../views/fpdj/xsfphc.vue"),
        meta: {
          title: "销售发票(红冲)",
        },
      },
      {
        path: "/CaidanManagement",
        name: "CaidanManagement",
        component: () => import("../views/fpdj/xsfpth.vue"),
        meta: {
          title: "销售发票(退货)",
        },
      },
      {
        path: "/BumenManagement",
        name: "BumenManagement",
        component: () => import("../views/fpdj/cgfp.vue"),
        meta: {
          title: "采购发票",
        },
      },
      {
        path: "/CodeCheckRule",
        name: "CodeCheckRule",
        component: () => import("../views/fpdj/cgfphc.vue"),
        meta: {
          title: "采购发票(红冲)",
        },
      },
      {
        path: "/roleUserList",
        name: "roleUserList",
        component: () => import("../views/fpdj/cgfpth.vue"),
        meta: {
          title: "采购发票(退货)",
        },
      },
    ],
  },
];

const router = new VueRouter({
  routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
  if(to.path === from.path){
    next(false);
  }
  if(to.path === '/login')
    return next();
  let token = localStorage.getItem('token');
  if(!token)
    return next('/login');
  next();
});
router.beforeEach((to, from, next) => {
  document.title = to.meta.title;
  next();
});

export default router
